package com.jn.zhinongbao.adapter.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.supportingagriculturaltreasures.PublishFarmerCircleActivity;
import com.example.supportingagriculturaltreasures.R;
import com.example.supportingagriculturaltreasures.adapter.FarmerCircleAdapter;
import com.example.supportingagriculturaltreasures.bean.FarmerCircle;
import com.example.supportingagriculturaltreasures.database.DatabaseHelper;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;
import java.util.List;

public class FarmerCircleFragment extends Fragment implements FarmerCircleAdapter.OnFarmerCircleActionListener {

    private RecyclerView recyclerView;
    private SwipeRefreshLayout swipeRefreshLayout;
    private FloatingActionButton fabAdd;
    private LinearLayout tvEmpty;
    private FarmerCircleAdapter adapter;
    private List<FarmerCircle> circleList;
    private DatabaseHelper databaseHelper;

    public static FarmerCircleFragment newInstance() {
        return new FarmerCircleFragment();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_farmer_circle, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        initViews(view);
        initData();
        setupRecyclerView();
        setupClickListeners();
        loadFarmerCircleData();
    }

    private void initViews(View view) {
        recyclerView = view.findViewById(R.id.recycler_view);
        swipeRefreshLayout = view.findViewById(R.id.swipe_refresh);
        fabAdd = view.findViewById(R.id.fab_add);
        tvEmpty = view.findViewById(R.id.tv_empty);
    }

    private void initData() {
        databaseHelper = new DatabaseHelper(getContext());
        circleList = new ArrayList<>();
    }

    private void setupRecyclerView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(layoutManager);
        
        adapter = new FarmerCircleAdapter(circleList, this);
        recyclerView.setAdapter(adapter);
        
        // 下拉刷新
        swipeRefreshLayout.setOnRefreshListener(this::loadFarmerCircleData);
    }

    private void setupClickListeners() {
        fabAdd.setOnClickListener(v -> {
            Intent intent = new Intent(getActivity(), PublishFarmerCircleActivity.class);
            startActivity(intent);
        });
    }

    private void loadFarmerCircleData() {
        List<FarmerCircle> circles = databaseHelper.getAllFarmerCircles();
        
        circleList.clear();
        circleList.addAll(circles);
        
        if (circleList.isEmpty()) {
            recyclerView.setVisibility(View.GONE);
            tvEmpty.setVisibility(View.VISIBLE);
        } else {
            recyclerView.setVisibility(View.VISIBLE);
            tvEmpty.setVisibility(View.GONE);
        }
        
        adapter.notifyDataSetChanged();
        swipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void onResume() {
        super.onResume();
        loadFarmerCircleData(); // 从发布页面返回时刷新数据
    }

    @Override
    public void onLikeClick(FarmerCircle circle) {
        // 处理点赞逻辑
        if (circle.isLiked()) {
            // 取消点赞
            databaseHelper.unlikeFarmerCircle(circle.getId());
            circle.setLiked(false);
            circle.setLikeCount(circle.getLikeCount() - 1);
        } else {
            // 点赞
            databaseHelper.likeFarmerCircle(circle.getId());
            circle.setLiked(true);
            circle.setLikeCount(circle.getLikeCount() + 1);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onCommentClick(FarmerCircle circle) {
        // 处理评论逻辑，暂时显示Toast
        // TODO: 实现评论功能
    }

    @Override
    public void onShareClick(FarmerCircle circle) {
        // 处理分享逻辑
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        shareIntent.putExtra(Intent.EXTRA_TEXT, "分享农友圈：" + circle.getContent());
        startActivity(Intent.createChooser(shareIntent, "分享到"));
    }

    @Override
    public void onItemClick(FarmerCircle circle) {
        // 跳转到农友圈详情页
        Intent intent = new Intent(getActivity(), com.example.supportingagriculturaltreasures.FarmerCircleDetailActivity.class);
        intent.putExtra("circle_id", circle.getId());
        intent.putExtra("circle_content", circle.getContent());
        intent.putExtra("circle_author", circle.getAuthorName());
        intent.putExtra("circle_location", circle.getLocation());
        intent.putExtra("circle_time", circle.getCreateTime());
        intent.putExtra("circle_image_url", circle.getImageUrl());
        intent.putExtra("circle_like_count", circle.getLikeCount());
        intent.putExtra("circle_comment_count", circle.getCommentCount());
        startActivity(intent);
    }
} 